﻿$(document).ready(function(){
	config.loadData("query");
});
/**
 * 选中子元素
 * @param obj
 */
function check(obj){
	if(obj.checked){
		var checkbox = $("[name='web']");
		checkbox.attr("checked",'true');//全选 
		$("[class='tableContent']").css("background-color","#D8F79D");
	}else{
		$("[name='web']").removeAttr("checked");//取消全选 
		$("[class='tableContent']").css("background-color","");
	}
}
/**
 * 当前选中项
 * @param obj
 */
function checkedInput(obj){
	var id = obj.id.replace("td","");
	if(obj.checked)
		$("#web"+id).css("background-color","#D8F79D");
	else
		$("#web"+id).css("background-color","");
		
}
/**
 * 单击行时，选中的则取消，未选中的则选中
 * @param obj
 */
function ischecked(obj){
	var id = obj.id;
	var trObj = $("#"+id);
	var tdId = "td"+id.replace("web","");
	var objCheck = $("#"+tdId);
	if(objCheck.attr("checked")){
		objCheck.removeAttr("checked");//取消选中 
		trObj.css("background-color","");
	}else{
		objCheck.attr("checked",'true');//选中
		trObj.css("background-color","#D8F79D");
	}
}

var config = {
	addDialog : function(uri,d) {
		var kdialog = KindEditor.dialog({
			width : 400,
			height : 200,
			title : '添加全局配置信息',
			body : '<div id="txt_source_div" style="padding:20px;height:200px;overflow:auto;overflow-x:hidden;">'+(uri=="insert"?config.addHtml():config.editHtml(d))+'</div>',
			shadowMode : true,
			closeBtn : {name : '关闭',click : function(e) {kdialog.remove();}},
			yesBtn : {
				name : '保存',
				click : function(e) {
					 _waiting._show();
					/**
					 * 提交保存信息数据
					 */
					if(formIsNull($("#form1"))){
						$("#form1").ajaxSubmit({
							url:uri,
							type:'post',
							success:function(data){
								var res = "";
								
								if(data > 0){
									res = "数据保存成功！！";
									kdialog.remove();
									config.loadData("query");
								}else{
									res = "保存保存失败！！";
								}
								$.growlUI(res);
								_waiting._hide();
							},
							error:function(msg){
								_waiting._hide();
								$.growlUI('数据保存失败');
							}
						});
					}
					_waiting._hide();
				}
			},
			noBtn : {name : '取消',click : function(e) {kdialog.remove();}}
		});
	},
	backgroundDiv : function(obj) {
		var div_id = obj.id;
		if (obj.checked) {
			jQuery("#import_txt_id" + div_id).css("background", "#ccc");
		} else {
			jQuery("#import_txt_id" + div_id).css("background", "#ffffff");
		}
	},
	addHtml : function(){
		var table = '<form action="addSysConfig" method="post" name="form1" id="form1">';
			table += '<div class="tab-page" id="baseinfo" style="display: block;">';
			table += '<table width="630" border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder">';
		    table += '<tr><td width="55" height="50">键(key):</td><td><input class="text" name="fkey" type="text" id="fkey" value="" size="15" style="border: 0;border-bottom: 1px solid #ABADB3;"/></td></tr>';
		    table += '<tr><td height="50">值(value):</td><td height="30"><input class="text" name="fvalue" type="text" id="fvalue" value="" size="15" style="border: 0;border-bottom: 1px solid #ABADB3;"/>(该值只能为数字)</td></tr>';
		    table += '</table></div></form>';
	    return table;
	},
	loadData:function(uri,param){
		$.ajax({
			url:uri,
			type:"post",
			dataType:"json",
			data:param,
			success:function(data){
				if(param){
					if(uri=="delete"){
						res = "成功删除"+data+"条数据";
						$.growlUI(res);
					}else
						config.addDialog("update",data);
				}else
					config.bindGrid(data);
			},
			error:function(data){
				$.growlUI("数据加载出错"); 
			}
		});
	},
	bindGrid:function(data){
		var html = "";
		if(data){
			$.each(data,function(i,sys){
				html +='<tr id="web'+i+'" class="tableContent" onclick="ischecked(this)">';
				html +='<td width="6%" align="center" style="padding-left: 5px;"><input type="checkbox" name="web" onchange="checkedInput(this)" onclick="ischecked(document.getElementById(\'web'+i+'\'))" id="td'+ i +'" value="'+sys.id+'"/></td>';
				html +='<td width="47%" align="center">'+sys.fkey+'</td>';
				html +='<td width="47%" align="center">'+sys.fvalue+'</td></tr>';
			});
		}else{
			html = "<tr><td colspan='3' align='center'>暂无数据</td></tr>";
		}
		$("#data").html(html);
	},
	editData:function(){
		var ids = config.checkedInfo();
		if(ids.length == 1){
			config.loadData("queryById",{"id":ids[0]});
		}else if(ids.length == 0){
			var html = "<div style='vertical-align: middle;'><img src='/CMS/images/box/icon_alert.gif' alt='提示'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请选择要修改的信息</div>";	
			var	footerhtml='<input type="image" alt="Cancel" src="/CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'editHtmlcloseBtn\').click();"/>';
			Box("editHtml",10,10,html,footerhtml,901);
		}else if(ids.length > 1){
			var html = "<div style='vertical-align: middle;'><img src='/CMS/images/box/icon_alert.gif' alt='提示'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;只能修改1条信息！</div>";
			var	footerhtml='<input type="image" alt="Cancel" src="/CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'editHtmlcloseBtn\').click();"/>';
			Box("editHtml",10,10,html,footerhtml,901);
		}
	},
	checkedInfo:function(){
		var ids = [];
		$("[name='web'][checked]").each(function(){
			ids.push($(this).val());  
		}); 
		return ids;  
	},
	editHtml:function(data){
		var table = '<form action="addSysConfig" method="post" name="form1" id="form1">';
			table += '<div class="tab-page" id="baseinfo" style="display: block;">';
			table += '<table width="630" border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder">';
		    table += '<tr><td width="55" height="50">键(key):</td><td><input class="text" name="fkey" type="text" id="fkey" value="'+data.fkey+'" size="15" style="border: 0;border-bottom: 1px solid #ABADB3;"/><input name="id" type="hidden" value="'+data.id+'"/></td></tr>';
		    table += '<tr><td height="50">值(value):</td><td height="30"><input class="text" name="fvalue" type="text" id="fvalue" value="'+data.fvalue+'" size="15" style="border: 0;border-bottom: 1px solid #ABADB3;"/>(该值只能为数字)</td></tr>';
		    table += '</table></div></form>';
	    return table;
	},
	remove:function(){
		var ids = config.checkedInfo();
		if(ids.length > 0){
			if(confirm("确定要删除该信息吗？删除后无法恢复！")){
				var s = "";
			    for(var i=0;i<ids.length ;i++){
			        s +=ids[i]+",";
			    }
			    if(s != ""){
			    	s = s.substring(0,s.length-1);
			    }
				config.loadData("delete",{"ids":s});
				config.loadData("query");
				$("input[name=weball]").attr("checked",false);
			}
		}else{
			var html = "<div style='vertical-align: middle;'><img src='/CMS/images/box/icon_alert.gif' alt='提示'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请选择要删除的信息</div>";
			var	footerhtml='<input type="image" alt="Cancel" src="/CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'deleteHtmlcloseBtn\').onclick();"/>';
			Box("deleteHtml",10,10,html,footerhtml,901);
		}
	}
};